//
//  AddressMessageViewController.m
//  EncryptionTPB
//
//  Created by LvJianfeng on 15/9/16.
//  Copyright © 2015年 coderjianfeng. All rights reserved.
//

#import "AddressMessageViewController.h"
#import "UIScrollView+PullScale.h"
#import "UINavigationBar+Awesome.h"
#import "JFUtil.h"

@interface AddressMessageViewController ()<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) UITableView *tableView;
@end

@implementation AddressMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.navigationController.delegate = self;
    // Do any additional setup after loading the view.
    [self.view insertSubview:self.tableView atIndex:0];
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    if (self.tapContact.imageData==nil) {
        [dict setObject:[UIImage imageNamed:@"icon-avatar"] forKey:@"AVATAR"];
    }else{
        [dict setObject:[UIImage imageWithData:self.tapContact.imageData] forKey:@"AVATAR"];
    }
    [dict setObject:[CNContactFormatter stringFromContact:self.tapContact style:CNContactFormatterStyleFullName] forKey:@"NAME"];
    
    [self.tableView addPullScaleFuncInVC:self imgName:@"car.jpg" originalHeight:180 hasNavBar:NO value:dict];//(self.navigationController==nil)
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //开启ios右滑返回
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
    
    [self.navigationController.navigationBar setCustomBackgroundColor:[UIColor clearColor]];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 30, 30);
    backButton.layer.cornerRadius = 15;
    backButton.backgroundColor = [UIColor whiteColor];
    [backButton addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:YES];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
    
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    editButton.frame = CGRectMake(0, 0, 28, 28);
    editButton.layer.cornerRadius = 1;
    //editButton.clipsToBounds = YES;
    editButton.backgroundColor = [UIColor whiteColor];
    [editButton addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:YES];
    [editButton setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
    
    UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithCustomView:editButton];
    self.navigationItem.rightBarButtonItem = editItem;
}
//
//- (void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)popViewControllerAnimated:(UIButton*)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 3;
            break;
        case 2:
            return 2;
            break;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressMessageCell *cell = [AddressMessageCell cellForTableView:tableView];
    /*
     "<CNLabeledValue: 0x7fdbf87f7480: identifier=59D688EE-146A-49E6-96A9-19FF1640D302, label=_$!<Mobile>!$_, value=<CNPhoneNumber: 0x7fdbf87f6e80: countryCode=us, digits=5555648583>>",
     "<CNLabeledValue: 0x7fdbf87f8370: identifier=C4161E92-AD59-41BE-801D-8E594B363C32, label=_$!<Main>!$_, value=<CNPhoneNumber: 0x7fdbf87f82a0: countryCode=us, digits=4155553695>>"
     */
    switch (indexPath.section) {
        case 0:
            cell.tipLabel.text = @"手机/iPhone";
            cell.descrptionLabel.text = [(CNPhoneNumber*)self.tapContact.phoneNumbers[0].value stringValue];
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                    cell.tipLabel.text = @"公司/Company";
                    if (isNullString(self.tapContact.organizationName)) {
                        cell.descrptionLabel.text = @" ";
                    }else{
                        cell.descrptionLabel.text = self.tapContact.organizationName;
                    }
                    break;
                case 1:
                {
                    cell.tipLabel.text = @"部门/Department";
                    cell.descrptionLabel.text = self.tapContact.departmentName;
                }
                    break;
                case 2:
                    cell.tipLabel.text = @"职位/Position";
                    cell.descrptionLabel.text = self.tapContact.jobTitle;
                    break;
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            switch (indexPath.row) {
                case 0:
                    cell.tipLabel.text = @"邮箱/E-mail";
                    if (self.tapContact.emailAddresses.count>0) {
                        cell.descrptionLabel.text =self.tapContact.emailAddresses[0].value;
                    }
                    break;
                case 1:
                    cell.tipLabel.text = @"地址/Address";
                    if (self.tapContact.postalAddresses.count>0) {
                        CNPostalAddress *address = self.tapContact.postalAddresses[0].value;
                        cell.descrptionLabel.text = [NSString stringWithFormat:@"%@%@%@",address.state,address.city,address.street];
                    }
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    return cell;
}


#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
            NSLog(@"123123123");
            break;
            
        default:
            break;
    }
}

#pragma mark lazyload
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //_tableView.allowsSelection = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}
@end

@implementation AddressMessageCell
+ (AddressMessageCell *)cellForTableView:(UITableView*)tableView{
    static NSString *identifier = @"ADDRESSMESSAGECELL";
    AddressMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[AddressMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        TTTAttributedLabel *tipLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH-30, 15)];
        tipLabel.font = [UIFont systemFontOfSize:13];
        tipLabel.textColor = [UIColor customSystemColor];
        [self.contentView addSubview:tipLabel];
        self.tipLabel = tipLabel;
        
        TTTAttributedLabel *descrptionLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(15, 32, SCREEN_WIDTH-30, 20)];
        descrptionLabel.font = [UIFont systemFontOfSize:15];
        tipLabel.textColor = [UIColor customGrayColor];
        [self.contentView addSubview:descrptionLabel];
        self.descrptionLabel = descrptionLabel;
    }
    return self;
}
@end
